package it.nextgo.BusMate.task;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import it.nextgo.BusMate.model.PropostaModel;

import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

public class GetPropostaTask extends AsyncTask<String, Void, ArrayList<PropostaModel>> {

	Context context;
	ConnectivityManager con;
	NetworkInfo net;
	
	public GetPropostaTask(Context applicationContext) {
		context=applicationContext;
		con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		net = con.getActiveNetworkInfo();
	}
	
	
	@Override
	protected ArrayList<PropostaModel> doInBackground(String... urls) {
		if (net != null && net.isConnectedOrConnecting()) {
			RestTemplate restTemplate = new RestTemplate();
			Map<String, String> vars = Collections.singletonMap("numeroLinea", urls[0]);
			restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

			PropostaModel[] data = restTemplate.getForObject("http://busmatedev.appspot.com/getProposta?numeroLinea={numeroLinea}", PropostaModel[].class,vars);
			List<PropostaModel> list = Arrays.asList(data);
            ArrayList<PropostaModel> propostas = new ArrayList<PropostaModel>(list);
            return propostas;
		}
		return null;
	}
	

}
